import { Logger } from "@nestjs/common"

import { RequestClient } from '../../infra/common/request.client'
import { env } from '../../settings'


export class WeChatService {
  private static readonly logger = new Logger(WeChatService.name);

  static async getWechatInfo(code: string): Promise<any> {
    const url = `${env.WX_URL}/${env.WX_SESSION_ENDPOINT}?grant_type=${env.WX_GRANT_TYPE}&appid=${env.WX_APP_ID}&secret=${env.WX_SECRET}&js_code=${code}`;
    this.logger.log(`Request wechat url: ${url}`);

    const response = await RequestClient.get(url);
    try {
      if (response.status !== 200) {
        this.logger.error(`Request wechat error: ${response.statusText}`);
        return null
      }
      return { ...response.data };
    } catch (error) {
      this.logger.error(`Request wechat error: ${error.message}`);
    }
    return null
  }
}
